home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / DIALOGUT / DIALOGUT.C next >
Text File  |  1990-09-05  |  3KB  |  127 lines

  1. /*
  2.  * DialogUtils.c
  3.  *
  4. From engber@gumball.ils.nwu.edu Tue Apr 17 12:15:17 1990
  5. From: engber@gumball.ils.nwu.edu (Mike Engber)
  6. Newsgroups: comp.sys.mac.programmer
  7. Subject: Centering DLOGs & ALRTs - new & improved source
  8. Summary: multiple monitor support & better vertical placement
  9. Date: 16 Apr 90 22:11:08 GMT
  10. Reply-To: engber@gumball (Mike Engber)
  11. Organization: The Institute for the Learning Sciences
  12.  
  13. I've fixed up my DLOG/ALRT centering code along the lines suggested by
  14. Alex Kazim from Apple.
  15.  
  16.   1) They are now vertically placed so that 1/3 of the open space is above.
  17.  
  18.   2) If there are multiple monitors they will show up on the screen which
  19.      contains the cursor. I could have just as easily gone for the screen
  20.      with the active window, but I used GetMouse() since you normally interact
  21.      with DLOGs & ALRTs via the mouse.
  22.  
  23. Attached are the relevant portions of DialogUtils.h & DialogUtils.c.
  24. Permission  granted to do what you want with the code (even building
  25. nuclear weapons)
  26.  
  27. -ME
  28.  */
  29.  
  30. #include "DialogUtils.h"
  31. #include <MemoryMgr.h>
  32. #include <OSUtil.h>
  33. #include <StdFilePkg.h>
  34. #include <Color.h>
  35.  
  36. static Rect ScreenBounds(void)
  37. {
  38.     register BitMap *myScreenBits;
  39.  
  40.     asm {
  41.         move.l CurrentA5, a0
  42.         move.l (a0), myScreenBits
  43.         add.l  #-122, myScreenBits
  44.     }
  45.     return myScreenBits->bounds;
  46. }
  47.  
  48. static Rect DU_MouseGDRect(void){
  49. /* Returns the gdRect of the screen device the mouse is currently in. */
  50.     Point       p;
  51.     Rect        r = ScreenBounds();
  52.     GDHandle    curDevice;
  53.     SysEnvRec    world;
  54.  
  55.     if (SysEnvirons(curSysEnvVers, &world) == noErr && world.hasColorQD) {
  56.         curDevice = GetDeviceList();
  57.         GetMouse(&p);
  58.         while(curDevice){
  59.             if( TestDeviceAttribute(curDevice,screenDevice) &&
  60.                 TestDeviceAttribute(curDevice,screenActive) &&
  61.                 PtInRect(p,&(*curDevice)->gdRect)){
  62.                     r = (*curDevice)->gdRect;
  63.             }
  64.             curDevice = GetNextDevice(curDevice);
  65.         }
  66.     }
  67.     return r;
  68. }
  69.  
  70. static void DU_CenterRect(Rect* rect_p){
  71. /*
  72.  * Alligns *rect_p wrt screenBits.bounds - LR centered & with 1/3 of the
  73.  *  empty space above the rect.
  74.  */
  75.     Rect bRect = DU_MouseGDRect();
  76.  
  77.     bRect.top += GetMBarHeight();
  78.  
  79.     /* exactly centered */
  80.     OffsetRect(rect_p,
  81.         (bRect.right + bRect.left)/2 - (rect_p->right + rect_p->left)/2,
  82.         (bRect.top + bRect.bottom)/2 - (rect_p->top + rect_p->bottom)/2);
  83.     
  84.     /* 1/2 empty space above -> 1/3 empty space above */
  85.     OffsetRect(rect_p,0,-(rect_p->top - bRect.top)/3);
  86. }
  87.  
  88. static Point DU_Where(short rsrcId){
  89. /*
  90.  * Returns centering point for the topLeft corner of a dialog.
  91.  */
  92.     Handle  h = GetResource('DLOG',rsrcId);
  93.     Rect    r = {0,0,0,0};
  94.     if(h){
  95.         r = *((Rect*)(*h));
  96.         DU_CenterRect(&r);
  97.         ReleaseResource(h);
  98.     }
  99.     return topLeft(r);
  100. }
  101.  
  102. Point DU_StdPutWhere(void){
  103.     return DU_Where(putDlgID);
  104. }
  105.  
  106. Point DU_StdGetWhere(void){
  107.     return DU_Where(getDlgID);
  108. }
  109.  
  110. static short DU_Center(ResType type, short rsrcId){
  111. /*
  112.  * Reads in an 'ALRT' or 'DLOG' rsrc template & centers it's display Rect.
  113.  */
  114.     Handle  h = GetResource(type,rsrcId);
  115.     if(h) DU_CenterRect((Rect*)(*h));
  116.     return rsrcId;
  117. }
  118.  
  119. short DU_CenterALRT(short rsrcId){
  120.     return DU_Center('ALRT',rsrcId);
  121. }
  122.  
  123. short DU_CenterDLOG(short rsrcId){
  124.     return DU_Center('DLOG',rsrcId);
  125. }
  126.  
  127.